package com.ctl.szmd.szmd.feature_wm.ui.adapter.wm

import android.view.View
import androidx.recyclerview.widget.DiffUtil
import com.ctl.lib_common.base.mvvm.DataBindingListAdapter
import com.ctl.lib_common.base.mvvm.DataBindingViewHolder
import com.ctl.szmd.szmd.feature_wm.R
import com.ctl.szmd.szmd.feature_wm.data.model.WmDataListModel
import com.ctl.szmd.szmd.feature_wm.databinding.WmAdapterWebAttachmentBinding

/**
 * created by : chentl
 * Date: 2020/07/30
 */
class WmWebAttachmentAdapter : DataBindingListAdapter<WmDataListModel>(diffCallback) {
    companion object {
        val diffCallback =
            object : DiffUtil.ItemCallback<WmDataListModel>() {
                override fun areItemsTheSame(
                    oldItem: WmDataListModel,
                    newItem: WmDataListModel
                ): Boolean {
                    return oldItem.attachmentNetPath == newItem.attachmentNetPath
                }

                override fun areContentsTheSame(
                    oldItem: WmDataListModel,
                    newItem: WmDataListModel
                ): Boolean {
                    return oldItem == newItem
                }

            }
    }

    override fun viewHolder(layout: Int, view: View): DataBindingViewHolder<WmDataListModel> {
        val holder =
            WmWebAttachmentViewHolder(
                view
            )
        holder.itemView.setOnClickListener { listener?.onClick(view) }
        return holder
    }

    override fun layout(position: Int): Int {
        return R.layout.wm_adapter_web_attachment
    }

    var listener: View.OnClickListener? = null


    class WmWebAttachmentViewHolder(view: View) : DataBindingViewHolder<WmDataListModel>(view) {
        private val binding: WmAdapterWebAttachmentBinding by viewHolderBinding(view)

        override fun bindData(data: WmDataListModel, position: Int) {
            binding.apply {
                model = data
                executePendingBindings()
            }
            itemView.tag = data
        }
    }

}

